<?php
/*
 * Created on Jan 23, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */


////////////////////////////////////////////////////////////////
// CODE IN THIS FILE SHOULD BE REVISED VERY SOON
// TO PUT DOCTRINE, MVC, AND LOGGER, SMARTY INTO MVC''S REGISTRY
// TO WORK TOGETHER 
////////////////////////////////////////////////////////////////




// Implement Object Relationship Mapping packages
$GLOBAL_LOGGER_DOCTRINE->info('Initiate DOCTRINE ORM Engine');

require_once GLOBAL_MODULE_DOCTRINE;

Doctrine::debug(true);
spl_autoload_register(array('Doctrine', 'autoload'));

setupDoctrine();


Doctrine_Manager::getInstance()->setAttribute('model_loading', 'conservative'); // 'conservative' 'aggressive'
Doctrine::loadModels(GLOBAL_MODULE_DOCTRINE_MODEL_BASE);
Doctrine::loadModels(GLOBAL_MODULE_DOCTRINE_MODEL);




$GLOBAL_LOGGER_DOCTRINE->info('Connect to database');
try {
    $dbh = new PDO(DSN, USER, PASSWD);
	//Phu Ha
	//set $dbh to registry? or somewhere at global level
} catch (PDOException $e) {
    $GLOBAL_LOGGER_DOCTRINE->fatal('Connection failed: ' . $e->getMessage());
}



$conn = Doctrine_Manager::getInstance()->openConnection($dbh);
//set $conn to registry? or somewhere at global level?









function setupDoctrine() {
	if (DOCTRINE_SETUP == '1') {
		try {
			$dbh = new PDO(DSN, USER, PASSWD);
			//Phu Ha
			//set $dbh to registry? or somewhere at global level
		} catch (PDOException $e) {
			$GLOBAL_LOGGER_DOCTRINE->fatal('Connection failed: ' . $e->getMessage());
		}
		Doctrine_Manager::connection($dbh);

		// import method takes one parameter : the import directory ( the directory where
		// the generated record files will be put in
		Doctrine::generateModelsFromDb(GLOBAL_MODULE_DOCTRINE_MODEL);
	}
}









/////////////////////////////////////////////////////////////////
// THESE SAMPLE CODE BELOW WILL BE REMOVED
/////////////////////////////////////////////////////////////////
function insertRecord() {
	$c[0] = '5';
	$q = new Doctrine_Query();
	$r = $q->from('ShippingRequest SR')->where('SR.request_id = ?', $c[0])->fetchArray();
	$num = $q->count();

	if (1>$num || null==$num) {
		$sl = new ShippingRequest();
		$sl->requestor_id = 10;
		$sl->customer_id = 10;
		$sl->request_date = date("Y-m-d");
		$sl->save(); 
	}	
}

function queryRecords() {
	$q = new Doctrine_Query ();
	$result = $q ->from('ShippingRequest SR')->fetchArray();

	echo "<br />Results Shipping Request:<br />";
	foreach($result as $row) {
		foreach($row as $column) {
			echo $column." | ";
		}
		echo "<br />";
	}
}
 
 
 
?>
